package com.lxmes.sales.service.impl;

import com.lxmes.common.exception.ServiceException;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.SecurityUtils;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.sales.domain.NcClientFiles;
import com.lxmes.sales.mapper.NcClientFilesMapper;
import com.lxmes.sales.service.INcClientFilesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.Validator;
import java.util.List;

/**
 * 客户档案Service业务层处理
 *
 * @author ruoyi
 * @date 2022-11-22
 */
@Service
public class NcClientFilesServiceImpl implements INcClientFilesService
{
    @Resource
    private NcClientFilesMapper ncClientFilesMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询客户档案
     *
     * @param id 客户档案主键
     * @return 客户档案
     */
    @Override
    public NcClientFiles selectNcClientFilesById(Long id)
    {
        return ncClientFilesMapper.selectNcClientFilesById(id);
    }
    @Override
    public NcClientFiles selectNcClientFilesByCode(String code)
    {
        return ncClientFilesMapper.selectNcClientFilesByCode(code);
    }

    /**
     * 查询客户档案列表
     *
     * @param ncClientFiles 客户档案
     * @return 客户档案
     */
    @Override
    public List<NcClientFiles> selectNcClientFilesList(NcClientFiles ncClientFiles)
    {
        ncClientFiles.setFactoryCode(SecurityUtils.getFactoryCode());
        return ncClientFilesMapper.selectNcClientFilesList(ncClientFiles);
    }
    /**
     * 新增客户档案
     *
     * @param ncClientFiles 客户档案
     * @return 结果
     */
    @Override
    public int insertNcClientFiles(NcClientFiles ncClientFiles)
    {
        ncClientFiles.setFactoryCode(SecurityUtils.getFactoryCode());
        ncClientFiles.setFactoryName(SecurityUtils.getFactoryName());
        ncClientFiles.setCreateTime(DateUtils.getNowDate());
        ncClientFiles.setCreateUser(SecurityUtils.getUserId()+"");
        ncClientFiles.setDeptId(SecurityUtils.getDeptId());
        ncClientFilesMapper.insertNcClientFiles(ncClientFiles);
        Long id = ncClientFiles.getId();
        String idNum = String.format("%06d", id);
        ncClientFiles.setClientCode(idNum);
       return ncClientFilesMapper.updateNcClientFiles(ncClientFiles);
    }

    /**
     * 修改客户档案
     *
     * @param ncClientFiles 客户档案
     * @return 结果
     */
    @Override
    public int updateNcClientFiles(NcClientFiles ncClientFiles)
    {
        return ncClientFilesMapper.updateNcClientFiles(ncClientFiles);
    }

    /**
     * 批量删除客户档案
     *
     * @param ids 需要删除的客户档案主键
     * @return 结果
     */
    @Override
    public int deleteNcClientFilesByIds(Long[] ids)
    {
        return ncClientFilesMapper.deleteNcClientFilesByIds(ids);
    }

    /**
     * 删除客户档案信息
     *
     * @param id 客户档案主键
     * @return 结果
     */
    @Override
    public int deleteNcClientFilesById(Long id)
    {
        return ncClientFilesMapper.deleteNcClientFilesById(id);
    }

    @Override
    public String imporIteminfo(List<NcClientFiles> userList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(userList) || userList.size() == 0)
        {
            throw new ServiceException("导入物料数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (NcClientFiles clients : userList)
        {
            try
            {
                BeanValidators.validateWithException(validator, clients);
                clients.setCreateBy(operName);
                clients.setFactoryCode(SecurityUtils.getFactoryCode());
                clients.setFactoryName(SecurityUtils.getFactoryName());
                clients.setUpdateBy(operName);
                this.insertNcClientFiles(clients);
                successNum++;
                successMsg.append("<br/>" + successNum + "、客户 " + clients.getClientName() + " 导入成功");
               /* if (StringUtils.isNull(u))
                {
                    BeanValidators.validateWithException(validator, iteminfo);
                    iteminfo.setCreateBy(operName);
                    iteminfo.setUpdateBy(operName);
                    iteminfo.setTenantId(SecurityUtils.getDeptId()+"");
                    this.insertJcItembzinfo(iteminfo);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、标准 " + iteminfo.getItemBzName() + " 导入成功");
                }
                else
                {
                    BeanValidators.validateWithException(validator, iteminfo);
                    iteminfo.setUpdateBy(operName);
                    this.updateJcItembzinfo(iteminfo);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、数据 " + iteminfo.getItemBzName()+ " 更新成功");
                }*/
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + clients.getClientName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

}
